#include <gflags/gflags.h>
#include <spdlog/async.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/spdlog.h>

#include "coin2/strategy/vmm/vmm.h"

DEFINE_string(driver, "", "driver config");
DEFINE_string(config, "", "strategy config");
DEFINE_bool(verbose, false, "verbose logging");

int main(int argc, char* argv[]) {
  google::InitGoogleLogging(argv[0]);
  gflags::ParseCommandLineFlags(&argc, &argv, true);

  auto path = std::experimental::filesystem::path(FLAGS_driver);
  coin2::app::DriverConfig app_config;
  app_config.MergeFrom(coin2::base::config::DriverConfigFromJson(path));

  // split log files every 1 GiB / at most 5 files
  const std::string& strat_name = (*app_config.mutable_strategy()).strategy_name();
  auto rotating_async_logger = spdlog::rotating_logger_mt<spdlog::async_factory>(
      "async_file_logger",
      fmt::format("{}/log/{}", std::getenv("HOME"), strat_name),
      1048576 * 1024,
      4);
  spdlog::set_default_logger(rotating_async_logger);
  // flush every 5 sec
  spdlog::flush_every(std::chrono::seconds(5));
  spdlog::set_pattern("%Y%m%d.%H%M%S.%f [%s:%!:%#] %v");

  SPDLOG_INFO("app_config: {}", app_config.DebugString());

  coin2::strategy::vmm::VmmStrategy strategy(FLAGS_config, GetCurrentTimestamp(), FLAGS_verbose);
  coin2::app::Driver driver(app_config, &strategy);
  driver.RegisterCleanupHandler();
  driver.Run();
  return 0;
}
